package com.swpu.service_drink.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_drink.entity.DrinkRecord;
import com.swpu.service_drink.entity.vo.DrinkCountChartVo;
import com.swpu.service_drink.entity.vo.DrinkRecordChartDataVo;
import com.swpu.service_drink.mapper.DrinkRecordMapper;
import com.swpu.service_drink.service.DrinkRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-02-28
 */
@Service
public class DrinkRecordServiceImpl extends ServiceImpl<DrinkRecordMapper, DrinkRecord> implements DrinkRecordService {

    @Override
    public void addDrinkRecord(String openid, DrinkRecord drinkRecord) {
        drinkRecord.setUserId(openid);
        boolean isSave = this.save(drinkRecord);
        if (!isSave) throw new RuntimeException("保存用户饮水数据失败");
    }

    @Override
    public List<DrinkRecord> getDrinkRecordDataByDate(String openid, Date queryDate) {
        LambdaQueryWrapper<DrinkRecord> queryWrapper = new LambdaQueryWrapper<>();
        DateTime beginOfDay = DateUtil.beginOfDay(queryDate);
        DateTime endOfDay = DateUtil.endOfDay(queryDate);
        queryWrapper.eq(DrinkRecord::getUserId,openid);
        queryWrapper.between(DrinkRecord::getRecordTime,beginOfDay,endOfDay);
        List<DrinkRecord> drinkRecordList = this.list(queryWrapper);
        return drinkRecordList;
    }

    @Override
    public void deleteDrinkRecordData(String openid, String recordId) {
        boolean isRemove = this.removeById(recordId);
        if(!isRemove) throw new RuntimeException("删除饮水记录失败");
    }

    @Override
    public List<DrinkRecordChartDataVo> getDrinkDataToChart(String openid, Integer dateCount) {
        LambdaQueryWrapper<DrinkRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(DrinkRecord::getRecordTime);
        DrinkRecord recordFirst = this.getOne(queryWrapper);
        Date recentDate = DateUtil.endOfDay(recordFirst.getRecordTime());
        Calendar calendar = new GregorianCalendar();
        //将Date设置到Calendar中
        calendar.setTime(recentDate);
        //获得当前时间之前一周时间点
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - dateCount-1);
        DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss");
        //一周前的时间点
        DateTime beginDate = DateUtil.beginOfDay(calendar.getTime());
        List<DrinkRecordChartDataVo> recordChartDataVoList = baseMapper.getDrinkDataToChart(openid, beginDate, recentDate);
        ArrayList<Date> dates = new ArrayList<>();
        for (DrinkRecordChartDataVo drinkRecordChartDataVo : recordChartDataVoList) {
            dates.add(drinkRecordChartDataVo.getRecordDate());
        }
        Date dateTemp=beginDate;
        while (dateTemp.before(DateUtil.beginOfDay(recentDate))){
            for (Date date : dates) {
                if (date.getTime()!=dateTemp.getTime()){
                    DrinkRecordChartDataVo chartDataVoAdd = new DrinkRecordChartDataVo();
                    chartDataVoAdd.setRecordDate(dateTemp);
                    chartDataVoAdd.setVolumeSum(0);
                    recordChartDataVoList.add(chartDataVoAdd);
                }
                break;
            }
           dateTemp = DateUtil.offsetDay(dateTemp,1);
        }
        recordChartDataVoList.sort(Comparator.comparing(DrinkRecordChartDataVo::getRecordDate));
        return recordChartDataVoList;
    }

    @Override
    public List<DrinkCountChartVo> getDrinkCountToChart(String openid,  Integer dateCount) {
        LambdaQueryWrapper<DrinkRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(DrinkRecord::getRecordTime);
        DrinkRecord recordFirst = this.getOne(queryWrapper);
        Date recentDate = DateUtil.endOfDay(recordFirst.getRecordTime());
        Calendar calendar = new GregorianCalendar();
        //将Date设置到Calendar中
        calendar.setTime(recentDate);
        //获得当前时间之前一周时间点
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)-dateCount-1);
        DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss");
        //一周前的时间点
        DateTime beginDate = DateUtil.beginOfDay(calendar.getTime());
        List<DrinkCountChartVo> drinkCountChartVoList = baseMapper.getDrinkCountToChart(openid, beginDate, recentDate);
        return drinkCountChartVoList;
    }

    @Override
    public Map getDrinkVolumeByDate(String openid, Date queryDate) {
        Map map= new HashMap<String,Object>();
        LambdaQueryWrapper<DrinkRecord> queryWrapper = new LambdaQueryWrapper<>();
        DateTime beginOfDay = DateUtil.beginOfDay(queryDate);
        DateTime endOfDay = DateUtil.endOfDay(queryDate);
        queryWrapper.eq(DrinkRecord::getUserId,openid);
        queryWrapper.between(DrinkRecord::getRecordTime,beginOfDay,endOfDay);
        queryWrapper.orderByDesc(DrinkRecord::getRecordTime);
        List<DrinkRecord> drinkRecordList = this.list(queryWrapper);
        int volume=0;
        for (DrinkRecord drinkRecord : drinkRecordList) {
            volume+=1.0*drinkRecord.getVolume()*drinkRecord.getWaterPercentage()/100;
        }
        map.put("todayVolume",volume);
        if(drinkRecordList.size()>0){
            map.put("updateTime",drinkRecordList.get(0).getRecordTime());
        }else  map.put("updateTime",new Date());

        return map;

    }

    @Override
    public DrinkRecord getLastedDrinkRecordByDate(String openid,Date queryDate) {
        LambdaQueryWrapper<DrinkRecord> queryWrapper = new LambdaQueryWrapper<>();
        DateTime beginOfDay = DateUtil.beginOfDay(queryDate);
        DateTime endOfDay = DateUtil.endOfDay(queryDate);
        queryWrapper.eq(DrinkRecord::getUserId,openid).between(DrinkRecord::getRecordTime,beginOfDay,endOfDay).orderByDesc(DrinkRecord::getRecordTime);
        return this.getOne(queryWrapper);

    }
}
